package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
)

// EntUser holds the schema definition for the EntUser entity.
type EntUser struct {
	ent.Schema
}

// Fields of the EntUser.
func (EntUser) Fields() []ent.Field {

	return []ent.Field{

		field.Int32("id").SchemaType(map[string]string{
			dialect.MySQL: "int(10)unsigned", // Override MySQL.
		}).Comment("自增Id").Unique(),

		field.String("name").SchemaType(map[string]string{
			dialect.MySQL: "varchar(255)", // Override MySQL.
		}).Optional().Default("").Comment("名字"),

		field.Int32("age").SchemaType(map[string]string{
			dialect.MySQL: "int(10)unsigned", // Override MySQL.
		}).Optional().Default(0).Comment("年龄"),
	}

}

// Edges of the EntUser.
func (EntUser) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("cars", EntCar.Type), // 1:N user -> car 是一对多的关系

		edge.From("groups", EntGroup.Type).Ref("users"), // N:N user -> group 是多对多的关系
	}
}

// 指定表名
func (EntUser) Table() string {
	return "ent_users"
}
